package com.example.mybatisplus.web.controller;

import ch.qos.logback.classic.net.server.HardenedLoggingEventInputStream;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.model.dto.RepairForm;
import com.example.mybatisplus.model.dto.RoomAdjustForm;
import com.example.mybatisplus.service.RepairService;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.DormitoryAdminService;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *  前端控制器
 *
 *
 * @author zzy
 * @since 2024-06-24
 * @version v1.0
 */
@Controller
@RequestMapping("/api/dormitoryAdmin")
public class DormitoryAdminController {

    private final Logger logger = LoggerFactory.getLogger( DormitoryAdminController.class );

    @Autowired
    private DormitoryAdminService dormitoryAdminService;
    @Autowired
    private RepairService repairService;

    /**
     * 描述：根据Id 查询
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        DormitoryAdmin  dormitoryAdmin =  dormitoryAdminService.getById(id);
        return JsonResponse.success(dormitoryAdmin);
    }

    /**
     * 描述：根据Id删除
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        dormitoryAdminService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     *
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateDormitoryAdmin(DormitoryAdmin  dormitoryAdmin) throws Exception {
        dormitoryAdminService.updateById(dormitoryAdmin);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建DormitoryAdmin
     *
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(DormitoryAdmin  dormitoryAdmin) throws Exception {
        dormitoryAdminService.save(dormitoryAdmin);
        return JsonResponse.success(null);
    }


    @PostMapping("/selectInroomAdjust")
    @ResponseBody
    public JsonResponse selectInroomAdjust(@RequestBody Dot dot){
        dot.setPageNo(dot.getPageNo()-1);
        List<RoomAdjustForm> repairForms = dormitoryAdminService.selectInroomAdjust(dot);
        Map<String, Object> map = new HashMap<>();
        map.put("size", repairForms.size());
        return JsonResponse.success(repairForms, "success").setOtherData(map);

    }

    @PostMapping("/selectOutroomAdjust")
    @ResponseBody
    public JsonResponse selectOutroomAdjust(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RoomAdjustForm> RoomAdjustForms = dormitoryAdminService.selectOutroomAdjust(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", RoomAdjustForms.size());
        RoomAdjustForms = RoomAdjustForms.subList(start, Math.min(end, RoomAdjustForms.size()));
        return JsonResponse.success(RoomAdjustForms, "success").setOtherData(map);

    }
    @PostMapping("/selectInBuildingAdjust")
    @ResponseBody
    public JsonResponse selectInBuildingAdjust(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RoomAdjustForm> RoomAdjustForms = dormitoryAdminService.selectInBuildingAdjust(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", RoomAdjustForms.size());
        RoomAdjustForms = RoomAdjustForms.subList(start, Math.min(end, RoomAdjustForms.size()));
        return JsonResponse.success(RoomAdjustForms, "success").setOtherData(map);
    }
    @PostMapping("/selectAllocationInBuildingAdjust")
    @ResponseBody
    public JsonResponse selectAllocationInBuildingAdjust(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RoomAdjustForm> RoomAdjustForms = dormitoryAdminService.selectAllocationInBuildingAdjust(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", RoomAdjustForms.size());
        RoomAdjustForms = RoomAdjustForms.subList(start, Math.min(end, RoomAdjustForms.size()));
        return JsonResponse.success(RoomAdjustForms, "success").setOtherData(map);
    }
    @PostMapping("/confirmAdjust")
    @ResponseBody
    public JsonResponse confirmAdjust(@RequestBody Map<String, String> params){
        Long ID = Long.parseLong(params.get("ID"));
        Long roomID = Long.parseLong(params.get("roomID"));
        boolean b = dormitoryAdminService.confirmAdjust(ID, roomID);
        return JsonResponse.success(b, "Success");
    }
    @PostMapping("/selectRepair")
    @ResponseBody
    public JsonResponse selectRepair(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = repairService.selectRepair(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end,repairForms.size()));
        return JsonResponse.success(repairForms, "success").setOtherData(map);
    }
    @PostMapping("/selectDoneRepair")
    @ResponseBody
    public JsonResponse selectDoneRepair(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = repairService.selectDoneRepair(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end,repairForms.size()));
        return JsonResponse.success(repairForms, "success").setOtherData(map);
    }
//    @PostMapping("/allocateRepair")
//    @ResponseBody
//    public JsonResponse allocateRepair(@RequestBody Map<String, Integer> params){
//        boolean b = repairService.allocateRepair(params.get("ID"));
//        if(b)return JsonResponse.success(b, "Success in allocate repair.");
//        else return JsonResponse.failure("ID does not existed or has been deleted.");
//    }
    @PostMapping("/getOneRoom")
    @ResponseBody
    public JsonResponse getOneRoom(@RequestBody Map<String, Integer> params){
        Long id = Long.valueOf(params.get("ID"));
        Room room = dormitoryAdminService.getOneRoom(id);
        return JsonResponse.success(room, "Success");
    }
    @GetMapping("/getKeepers")
    @ResponseBody
    public JsonResponse getKeepers(){
        List<Keeper> keepers = dormitoryAdminService.getKeepers();
        return JsonResponse.success(keepers, "Success");
    }
    @PostMapping("/updateRepair")
    @ResponseBody
    public JsonResponse updateRepair(@RequestBody Map<String, Object> params){
        Long repairID = Long.parseLong((String)params.get("repairID"));
        Long keeperID = Long.parseLong((String)params.get("keeperID"));
        boolean b = dormitoryAdminService.updateRepair(repairID, keeperID);
        if(b)return JsonResponse.success(b, "Success in updating repair.");
        else return JsonResponse.failure("ID does not existed or has been deleted.");
    }
    @PostMapping("/getBuildingName")
    @ResponseBody
    public JsonResponse getBuildingName(@RequestBody Map<String, Integer> params){
        int id = params.get("ID");
        String buildingName = dormitoryAdminService.getBuildingName(id);
        Map<String, Object> map = new HashMap<>();
        map.put("name", buildingName);
        return JsonResponse.success(map, "Success");
    }
    @PostMapping("/insertActivity")
    @ResponseBody
    public JsonResponse insertActivity(@RequestBody Map<String,Object> params){
        Long id = Long.parseLong((String) params.get("dormitoryAdminID"));
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime checkTime=LocalDateTime.parse((String)params.get("checkTime"),df);
        dormitoryAdminService.insertActivity(id, checkTime);
        return JsonResponse.success("Success");
    }
    @PostMapping("/getAllActivity")
    @ResponseBody
    public JsonResponse getAllActivity(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<HygieneCheck> activities=dormitoryAdminService.getAllActivity(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", activities.size());
        activities = activities.subList(start, Math.min(end,activities.size()));
        return JsonResponse.success(activities, "Success").setOtherData(map);
    }
    @PostMapping("/getOneActivity")
    @ResponseBody
    public JsonResponse getOneActivity(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        dot.getData().put("checkTime",LocalDateTime.parse((String)dot.getData().get("checkTime")));
        List<HygieneCheck> activities=dormitoryAdminService.getOneActivity(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", activities.size());
        activities = activities.subList(start, Math.min(end,activities.size()));
        return JsonResponse.success(activities, "Success").setOtherData(map);
    }
    @PostMapping("/updateGrade")
    @ResponseBody
    public JsonResponse updateGrade(@RequestBody List<Map<String, Object>> params){
        for(Map<String, Object> map:params){
            Long id = Long.parseLong((String)map.get("id"));
            int grade =  Integer.parseInt((String) map.get("grade"));
            boolean b = dormitoryAdminService.updateGrade(id, grade);
            if(!b)return JsonResponse.failure("ID does not existed or has been deleted.");
        }
        return JsonResponse.success("Success");
    }
    @PostMapping("/updateGradeList")
    @ResponseBody
    public JsonResponse updateGradeList(@RequestBody Map<String, Object> params){
        Long dormitoryAdminID = Long.parseLong((String)params.get("dormitoryAdminID"));
        LocalDateTime checkTime = LocalDateTime.parse((String)params.get("checkTime"));
        for(Map<String, Object> obj:(List<Map<String, Object>>)params.get("dataList")){
            String roomName = obj.get("roomName").toString();
            int grade = (int) obj.get("grade");
            boolean b = dormitoryAdminService.updateGradeList(dormitoryAdminID,checkTime,roomName,grade);
            if(!b)return JsonResponse.failure("ID does not existed or has been deleted.");
        }
        return JsonResponse.success("Success");
    }
    @PostMapping("/getStudent")
    @ResponseBody
    public JsonResponse getStudent(@RequestBody Map<String, Object> params){
        Long dormitoryAdminID = Long.parseLong((String)params.get("id"));
        List<Student> students = dormitoryAdminService.getStudent(dormitoryAdminID);
        return JsonResponse.success(students, "Success");
    }
    @PostMapping("/createdIndiscipline")
    @ResponseBody
    public JsonResponse createdIndiscipline(@RequestBody Map<String, Object> params){
        Long dormitoryAdminID = Long.parseLong((String)params.get("dormitoryAdmin"));
        Long studentID = Long.parseLong((String)params.get("student"));
        String state = params.get("state").toString();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime occurrenceTime=LocalDateTime.parse((String)params.get("occurrenceTime"),df);
        String reason = params.get("reason").toString();
        boolean b = dormitoryAdminService.createdIndiscipline(dormitoryAdminID, studentID, state, occurrenceTime, reason);
        return b?JsonResponse.success("Success"):JsonResponse.failure("ID does not existed or has been deleted.");
    }
    @PostMapping("/getIndiscipline")
    @ResponseBody
    public JsonResponse getIndiscipline(@RequestBody Dot dot){
        dot.getData().put("dormitoryAdminID", Long.parseLong((String) dot.getData().get("dormitoryAdminID")));
        List<Indiscipline> indisciplines = dormitoryAdminService.getIndiscipline(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", indisciplines.size());
        indisciplines = indisciplines.subList(start, Math.min(end,indisciplines.size()));
        return JsonResponse.success(indisciplines, "Success").setOtherData(map);
    }
    @PostMapping("/selectInAllocationAdjust")
    @ResponseBody
    public JsonResponse selectInAllocationAdjust(@RequestBody Dot dot){
        List<RoomAdjustForm> roomAdjustForms = dormitoryAdminService.selectInAllocationAdjust(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", roomAdjustForms.size());
        roomAdjustForms = roomAdjustForms.subList(start, Math.min(end, roomAdjustForms.size()));
        return JsonResponse.success(roomAdjustForms, "Success").setOtherData(map);
    }
    @PostMapping("/selectOutAllocationAdjust")
    @ResponseBody
    public JsonResponse selectOutAllocationAdjust(@RequestBody Dot dot){
        List<RoomAdjustForm> roomAdjustForms = dormitoryAdminService.selectOutAllocationAdjust(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", roomAdjustForms.size());
        roomAdjustForms = roomAdjustForms.subList(start, Math.min(end, roomAdjustForms.size()));
        return JsonResponse.success(roomAdjustForms, "Success").setOtherData(map);
    }
    @PostMapping("/getDoneRepair")
    @ResponseBody
    public JsonResponse getDoneRepair(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = dormitoryAdminService.getDoneRepair(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end, repairForms.size()));
        return JsonResponse.success(repairForms, "Success").setOtherData(map);
    }
    @PostMapping("/getRooms")
    @ResponseBody
    public JsonResponse getRooms(@RequestBody Map<String, Object> params){
        Long dormitoryAdminID = Long.parseLong((String)params.get("ID"));
        List<Room> rooms = dormitoryAdminService.getRooms(dormitoryAdminID);
        return JsonResponse.success(rooms, "Success");
    }
    @PostMapping("/getHygieneByRoom")
    @ResponseBody
    public JsonResponse getHygieneByRoom(@RequestBody Dot dot){
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        dot.getData().put("roomID", Long.parseLong((String) dot.getData().get("roomID")));
        List<HygieneCheck> hygieneChecks= dormitoryAdminService.getHygieneByRoom(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", hygieneChecks.size());
        hygieneChecks = hygieneChecks.subList(start, Math.min(end, hygieneChecks.size()));
        return JsonResponse.success(hygieneChecks, "Success").setOtherData(map);
    }
    @PostMapping("/getIndisciplineByStudent")
    @ResponseBody
    public JsonResponse getIndisciplineByStudent(@RequestBody Dot dot){
        dot.getData().put("dormitoryAdmin", Long.parseLong((String) dot.getData().get("dormitoryAdmin")));
        dot.getData().put("student", Long.parseLong((String) dot.getData().get("student")));
        List<Indiscipline> indisciplines = dormitoryAdminService.getIndisciplineByStudent(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", indisciplines.size());
        indisciplines = indisciplines.subList(start, Math.min(end, indisciplines.size()));
        return JsonResponse.success(indisciplines, "Success").setOtherData(map);
    }
    @PostMapping("/updateRating")
    @ResponseBody
    public JsonResponse updateRating(@RequestBody Map<String, Object> params){
        Long dormitoryAdminID = Long.parseLong((String)params.get("dormitoryAdmin"));
        if(dormitoryAdminService.updateRating(dormitoryAdminID)){
            return JsonResponse.success("Success");
        }else{
            return JsonResponse.failure("ID does not existed or has been deleted.");
        }
    }
    @PostMapping("/getRating")
    @ResponseBody
    public JsonResponse getRating(@RequestBody Dot dot){
        dot.getData().put("dormitoryAdmin", Long.parseLong((String) dot.getData().get("dormitoryAdmin")));
        dot.getData().put("level", Integer.parseInt((String) dot.getData().get("level")));
        List<RatingTable> ratingTable = dormitoryAdminService.getRating(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", ratingTable.size());
        ratingTable = ratingTable.subList(start, Math.min(end, ratingTable.size()));
        return JsonResponse.success(ratingTable, "Success").setOtherData(map);
    }
}

